# -*- coding: utf-8 -*-
# coding=utf-8
'''
@author: Administrator
'''
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
parentdir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, parentdir)


import DBCenter

'''
    初始化style数据，分为10个城市
'''
def GenerateMerchant():
    seq = 326
    select_area_sql = '''
        SELECT * FROM `area`
    '''
    area_datas = DBCenter.rc_db.query(select_area_sql)
    for area_data in area_datas:
        insert_merchant_info = '''
            INSERT INTO `merchant_info`
                    (`merchant_id`,
                     `merchant_name`,
                     `password`,
                     `salt`,
                     `email`,
                     `cellphone`,
                     `phone`,
                     `area_id`,
                     `area_name`,
                     `province_id`,
                     `province_name`,
                     `city_id`,
                     `city_name`,
                     `district_id`,
                     `district_name`,
                     `address`,
                     `register_date`,
                     `register_ip`,
                     `last_login_date`,
                     `last_login_ip`,
                     `status`,
                     `create_user`,
                     `create_date`)
        VALUES ('%s',
                '%s',
                '48c64f4201dc4db15ed888f1519a14e2',
                'GGxk5G',
                '',
                '%s',
                '',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                NOW(),
                '',
                NOW(),
                '',
                1,
                'create_user',
                NOW());
        ''' % (
                str(seq),
                area_data.get('domain_name'),
                area_data.get('cellphone'),
                area_data.get('area_id'),
                area_data.get('area_name'),
                area_data.get('province_id'),
                area_data.get('province_name'),
                area_data.get('city_id'),
                area_data.get('city_name'),
                area_data.get('district_id'),
                area_data.get('district_name'),
                area_data.get('address')
               )
    
        DBCenter.rc_db.execute(insert_merchant_info)
    
        seq = seq + 1

def InsertStyle(merchant_data, style_data):
    insert_sql = '''
        INSERT INTO `style`
            (`style_id`,
             `style_name`,
             `title_promotion_text`,
             `brand_id`,
             `brand_name`,
             `sale_date`,
             `stop_sale_date`,
             `brief`,
             `description`,
             `html_desc`,
             `image`,
             `merchant_id`,
             `merchant_name`,
             `area_id`,
             `area_name`,
             `market_price`,
             `min_price`,
             `max_price`,
             `category_id1`,
             `category_name1`,
             `category_id2`,
             `category_name2`,
             `category_id3`,
             `category_name3`,
             `seo_title`,
             `seo_keyword`,
             `seo_description`,
             `sku_dimension_id1`,
             `sku_dimension_name1`,
             `sku_dimension_id2`,
             `sku_dimension_name2`,
             `sort`,
             `status`,
             `create_user`,
             `create_date`)
VALUES ('%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        %s,
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        %s,
        %s,
        %s,
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        '%s',
        %s,
        %s,
        'StyleInit',
        NOW());
    ''' % (
           merchant_data.get('merchant_name') + style_data.get('style_id'),
           style_data.get('style_name') if style_data.get('style_name') else 'NULL',
           style_data.get('title_promotion_text'),
           style_data.get('brand_id'),
           style_data.get('brand_name'),
           style_data.get('sale_date'),
           'NULL',
           style_data.get('brief'),
           style_data.get('description'),
           style_data.get('html_desc'),
           style_data.get('image'),
           merchant_data.get('merchant_id'),
           merchant_data.get('merchant_name'),
           merchant_data.get('area_id'),
           merchant_data.get('area_name'),
           style_data.get('market_price'),
           style_data.get('min_price'),
           style_data.get('max_price'),
           style_data.get('category_id1'),
           style_data.get('category_name1'),
           style_data.get('category_id2'),
           style_data.get('category_name2'),
           style_data.get('category_id3'),
           style_data.get('category_name3'),
           style_data.get('seo_title'),
           style_data.get('seo_keyword'),
           style_data.get('seo_description'),
           style_data.get('sku_dimension_id1'),
           style_data.get('sku_dimension_name1'),
           style_data.get('sku_dimension_id2'),
           style_data.get('sku_dimension_name2'),
           style_data.get('sort'),
           style_data.get('status')
           )
    print insert_sql
    DBCenter.rc_db.execute(insert_sql)
    
def InsertStyleImage(merchant_data, style_image_data):
    insert_sql = '''
        INSERT INTO `style_image`
                    (`style_id`,
                     `image_src`,
                     `sort`,
                     `description`,
                     `status`,
                     `create_user`,
                     `create_date`)
        VALUES ('%s',
                '%s',
                %s,
                '%s',
                %s,
                'StyleInit',
                NOW());
    ''' % (
           merchant_data.get('merchant_name') + style_image_data.get('style_id'),
           style_image_data.get('image_src'),
           style_image_data.get('sort'),
           style_image_data.get('description'),
           style_image_data.get('status'),
           )
    print insert_sql
    DBCenter.rc_db.execute(insert_sql)
    
def InsertItem(merchant_data, item_data, item_price):
    insert_sql = '''
        INSERT INTO `item`
                    (`item_id`,
                     `item_name`,
                     `item_desc`,
                     `style_id`,
                     `style_name`,
                     `product_id`,
                     `product_name`,
                     `sale_date`,
                     `stop_sale_date`,
                     `sku_dimension_id1`,
                     `sku_dimension_name1`,
                     `sku_dimension_value1`,
                     `sku_dimension_id2`,
                     `sku_dimension_name2`,
                     `sku_dimension_value2`,
                     `image`,
                     `instock_price`,
                     `market_price`,
                     `sale_price`,
                     `status`,
                     `uom`,
                     `sort`,
                     `create_user`,
                     `create_date`)
        VALUES ('%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                %s,
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                '%s',
                %s,
                %s,
                %s,
                %s,
                '%s',
                %s,
                'StyleInit',
                NOW());
    ''' % (
           merchant_data.get('merchant_name').upper() + item_data.get('item_id'),
           item_data.get('item_name'),
           item_data.get('item_desc'),
           merchant_data.get('merchant_name').upper() + item_data.get('style_id'),
           item_data.get('style_name'),
           item_data.get('product_id'),
           item_data.get('product_name'),
           item_data.get('sale_date'),
           'NULL',
           item_data.get('sku_dimension_id1'),
           item_data.get('sku_dimension_name1'),
           item_data.get('sku_dimension_value1'),
           item_data.get('sku_dimension_id2'),
           item_data.get('sku_dimension_name2'),
           item_data.get('sku_dimension_value2'),
           item_data.get('image'),
           item_data.get('instock_price'),
           item_data.get('market_price'),
           item_price.get('sale_price') if item_price else '0',
           item_data.get('status'),
           item_data.get('uom'),
           item_data.get('sort')
           )
    print insert_sql
    DBCenter.rc_db.execute(insert_sql)
     
def InsertItemImage():
    pass

def Run():
    #1. 按区域生成供应商
    #GenerateMerchant()
    
    
    #2. 获取merchant_info数据
    select_merchant_sql = '''
        SELECT * FROM `merchant_info`
    '''
    merchant_datas = DBCenter.rc_db.query(select_merchant_sql)
     
     
    #3. 获取style表数据
    select_style_sql = '''
        SELECT * FROM style
    '''
    style_datas = DBCenter.rc_db.query(select_style_sql)
     
    
    #4. 获取style_image表数据
    select_style_image_sql = '''
        SELECT * FROM style_image
    '''
    style_image_datas = DBCenter.rc_db.query(select_style_image_sql)
     
    
    #5. 获取item表数据
    select_item_sql = '''
        SELECT * FROM item
    '''
    item_datas = DBCenter.rc_db.query(select_item_sql)
    
    
    for merchant_data in merchant_datas:
#         #a.插入新的style数据
#         for style_data in style_datas:
#             InsertStyle(merchant_data, style_data)
        
        
#         #b.插入新的style_image数据
#         for style_image_data in style_image_datas:
#             InsertStyleImage(merchant_data, style_image_data)
             

#         #c.插入新的item数据
        for item_data in item_datas:
            select_item_price_sql = '''
                SELECT * FROM item_price
                WHERE item_id = '%s' 
                    AND `warehouse_name` = '%s'
            ''' % (
                    item_data.get('item_id'),
                    merchant_data.get('area_name')
                   )
            print select_item_price_sql
            item_price = DBCenter.rc_db.get(select_item_price_sql)
            InsertItem(merchant_data, item_data, item_price)
            

def InitStyleImage():
    select_style_image_sql = '''
        SELECT * FROM style_image
    '''
    style_image_datas = DBCenter.rc_db.query(select_style_image_sql)    
    
    for style_image_data in style_image_datas:
        dir_name = style_image_data.get('style_id')
        # 新增的数据该表
        if len(style_image_data.get('style_id')) > 6:
            dir_name = style_image_data.get('style_id')[2:]
        
        update_sql = '''
            UPDATE `style_image`
                SET `dir_name` = '%s'
            WHERE style_id = '%s' AND `image_src` = '%s'
        ''' % (
               dir_name,
               style_image_data.get('style_id'),
               style_image_data.get('image_src')
               )        
        DBCenter.rc_db.execute(update_sql)
    
Run()
#InitStyleImage()
print 'succ'

s = 'ZZMU0106'
print s[2:]   

